package online.foxnull2.foxmall.ware.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class MQConfig {

    @Bean
    public Exchange stockEventExchange() {
        TopicExchange exchange = new TopicExchange("stock-event-exchange", true, false);
        log.debug("stockEventExchange -> {}", exchange);
        return exchange;
    }

    @Bean
    public Queue stockDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "stock-event-exchange");
        arguments.put("x-dead-letter-routing-key", "stock.release.#");
        arguments.put("x-message-ttl", 120000);
        Queue queue = new Queue("stock.delay.queue", true, false, false, arguments);
        log.debug("stockDelayQueue -> {}", queue);
        return queue;
    }

    @Bean
    public Queue stockReleaseStockQueue() {
        Queue queue = new Queue("stock.release.stock.queue", true, false, false);
        log.debug("stockReleaseStockQueue -> {}", queue);
        return queue;
    }

    @Bean
    public Binding stockLockedBinding() {
        Binding binding = new Binding("stock.delay.queue", Binding.DestinationType.QUEUE, "stock-event-exchange", "stock.locked",
                null);
        log.debug("stockLockedBinding -> {}", binding);
        return binding;
    }

    @Bean
    public Binding stockReleaseBinding() {
        Binding binding = new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE, "stock-event-exchange", "stock.release.#",
                null);
        log.debug("stockReleaseBinding -> {}", binding);
        return binding;
    }
}
